home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 271_02 / exparg.doc < prev    next >
Text File  |  1988-01-27  |  5KB  |  95 lines

  1.  
  2.  
  3.         NAME
  4.                 expand_arg -- expand command line wildcard filenames
  5.  
  6.         SYNOPSIS
  7.                 nargc = expand_arg(argc, argv);
  8.                 int nargc;
  9.                 int argc;
  10.                 char *argv[];  (or char **argv, if you prefer)
  11.                 extern char **nargv;
  12.  
  13.  
  14.  
  15.         DESCRIPTION
  16.         In the UNIX (tm) world, the command line processor expands
  17.         wildcard filenames to all matching occurances and passes them
  18.         in (argc, argv) as though they had been entered discreetly
  19.         on the command line.  This makes wildcard expansion totally
  20.         transparent to the programmer.  Most implementations of
  21.         C comnpilers for MS-DOS (Turbo-C included) do NOT provide this
  22.         expansion.  Therefore, if one command line argument is "*.ASM",
  23.         this becomes one of the arguments to a program, instead of all
  24.         files which will match the expansion.  expand_arg() will take
  25.         the (argc, argv) passed through the main() function, and make a
  26.         NEW array, called (nargc, nargv) which will contain the expanded
  27.         parameters.  If there are no wildcards to expand, the new array
  28.         winds up being the same as the old array.  In addition, certain
  29.         arguments will NOT be expanded, as they will be considered possible
  30.         "options" or "literals".  Any argument beginning with a hyphen (-),
  31.         fraction bar (/), or single/double quote (" ') will not be expanded.
  32.         Arguments beginning with a reverse slash (\) will be assumed to
  33.         contain a directory name as part of a potential filename.
  34.         Arguments will be expanded if they contain either of the wildcard
  35.         characters (*) or (?).  No matches to the specification will
  36.         return no filenames.  A filename without a wildcard designator
  37.         will be passed to the new array without testing for existence.
  38.         Filenames expanded will, of course, exist, or they wouldn't have
  39.         been found.
  40.         The label "nargc" is not a mandatory label.  It is chosen merely
  41.         for convenience in designating the new argument count.  However,
  42.         the array name "nargv" is a requirement, since the objeect module
  43.         expand_arg defines that item.  If you use the file "smdefs.h",
  44.         the external reference to nargv will be done automatically, and
  45.         you needn't worry about it.
  46.         expand_arg() does dynamic memory allocation and re-allocation,
  47.         and therefore should be called as the FIRST function after
  48.         main(), and NEVER called again within the program.  Violation
  49.         of this requirement may cause some arguments to be lost if
  50.         this function has to reallocate memory during expansion.
  51.         Since wildcard expansion may result in more or less arguments
  52.         than originally passed, nargc will seldom equal argc.  It may be
  53.         either larger or smaller.
  54.         After calling expand_arg(), the variables nargc and nargv may be
  55.         used anywhere you would normally have used argc and argv.
  56.         If expand_arg() fails (usually a memory allocatiion problem),
  57.         it will return a value of 0.  No command line options would return 1,
  58.         since the program name itself is a command line argument.
  59.  
  60.         expand_arg()         page 2
  61.  
  62.  
  63.         EXAMPLE
  64.            #include <stdio.h>
  65.            #include <smdefs.h>
  66.            int nargc;
  67.  
  68.            main(argc, argv) int argc; char *argv[]; {
  69.               int i;
  70.               nargc = expand_arg(argc, argv);
  71.               if(nargc == 0) error("Memory allocation error");
  72.               for(i = 0; i < nargc; i++)
  73.                  printf("Argument %d is %s\n", i, nargv[i]);
  74.               }
  75.  
  76.  
  77.         SEE ALSO:
  78.            File TESTEXP.C contains a demo similar to the one above.
  79.  
  80.  
  81.         INTERESTING NOTE:
  82.           In a recent issue of Borland's "Turbo Technix" magazine, sent
  83.           to owners of a Turbo product, they contain the listings of
  84.           routines and modifications to do exactly what is done with
  85.           expand_arg().  However, they do NOT make exceptions for
  86.           option characters nor quoted strings, and their method is
  87.           much more complex to implement.  Occasionally, the "little guy"
  88.           can be smarter than the "big guy"!
  89.  
  90.  
  91.  
  92.         This function is found in SMTCx.LIB for the Turbo-C Compiler
  93.         This function introduced in Version 1.31 of SMTCx.LIB for
  94.         Turbo-C Version 1.5 only.
  95.